package com.yangchuang.openpicture.util;

import java.util.HashMap;
import java.util.Map;

public class BaseContext {
    // 存储一个Map，键为自定义key，值为任意类型
    private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();

    // 初始化Map（避免空指针）
    private static Map<String, Object> getOrInitMap() {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<>();
            threadLocal.set(map);
        }
        return map;
    }

    // 存值：添加一个键值对到Map
    public static void set(String key, Object value) {
        getOrInitMap().put(key, value);
    }

    // 取值：根据key从Map中获取
    public static Object get(String key) {
        return getOrInitMap().get(key);
    }

    // 移除某个键值对
    public static void remove(String key) {
        Map<String, Object> map = threadLocal.get();
        if (map != null) {
            map.remove(key);
        }
    }

    // 清空整个Map（避免内存泄漏）
    public static void clear() {
        threadLocal.remove();
    }
}
